package com.work.yshop.mvp.presenter;

import android.os.CountDownTimer;
import android.text.TextUtils;

import com.work.yshop.mvp.contract.RegisterContract;

import java.util.Random;

/**
 * 注册功能的Presenter实现类
 */
public class RegisterPresenter extends RegisterContract.Presenter {

    private CountDownTimer countDownTimer;
    private boolean isTimerRunning = false;
    private static final int COUNT_DOWN_TOTAL = 60000; // 60秒
    private static final int COUNT_DOWN_INTERVAL = 1000; // 1秒间隔

    /**
     * 生成随机验证码
     * @param length 验证码长度
     * @return 随机验证码
     */
    private String generateRandomCode(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        
        for (int i = 0; i < length; i++) {
            sb.append(random.nextInt(10)); // 0-9的随机数
        }
        
        return sb.toString();
    }

    @Override
    public void getVerificationCode(String phone) {
        if (TextUtils.isEmpty(phone)) {
            view.showPhoneError("请输入手机号");
            return;
        } 
        
        if (phone.length() != 11) {
            view.showPhoneError("请输入正确的手机号");
            return;
        }
        
        // 生成6位随机验证码
        String verificationCode = generateRandomCode(6);
        
        // 通知View验证码已发送
        view.onVerificationCodeSent(verificationCode);
        
        // 开始倒计时
        startCountDown();
    }

    @Override
    public void register(String phone, String verificationCode, String password, 
                        String confirmPassword, boolean isAgreementChecked) {
        
        // 验证手机号
        if (TextUtils.isEmpty(phone)) {
            view.showPhoneError("请输入手机号");
            return;
        } else if (phone.length() != 11) {
            view.showPhoneError("请输入正确的手机号");
            return;
        } else {
            view.showPhoneError(null);
        }

        // 验证验证码
        if (TextUtils.isEmpty(verificationCode)) {
            view.showVerificationCodeError("请输入验证码");
            return;
        } else if (verificationCode.length() != 6) {
            view.showVerificationCodeError("验证码应为6位数字");
            return;
        } else {
            view.showVerificationCodeError(null);
        }

        // 验证密码
        if (TextUtils.isEmpty(password)) {
            view.showPasswordError("请输入密码");
            return;
        } else if (password.length() < 6) {
            view.showPasswordError("密码长度至少为6位");
            return;
        } else {
            view.showPasswordError(null);
        }

        // 验证确认密码
        if (TextUtils.isEmpty(confirmPassword)) {
            view.showConfirmPasswordError("请确认密码");
            return;
        } else if (!password.equals(confirmPassword)) {
            view.showConfirmPasswordError("两次输入的密码不一致");
            return;
        } else {
            view.showConfirmPasswordError(null);
        }

        // 验证是否同意协议
        if (!isAgreementChecked) {
            view.showError("请先阅读并同意用户协议和隐私政策");
            return;
        }

        // 显示加载中
        view.showLoading();
        
        // 模拟网络请求
        // 在实际项目中，这里应该调用Model层的方法进行网络请求
        // 延迟1.5秒模拟网络请求
        new android.os.Handler().postDelayed(() -> {
            // 隐藏加载中
            view.hideLoading();
            
            // 注册成功
            view.onRegisterSuccess();
        }, 1500);
    }

    @Override
    public void startCountDown() {
        if (isTimerRunning) {
            return;
        }
        
        isTimerRunning = true;
        
        countDownTimer = new CountDownTimer(COUNT_DOWN_TOTAL, COUNT_DOWN_INTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (isViewAttached()) {
                    view.updateCountDownTimer((int) (millisUntilFinished / 1000));
                }
            }

            @Override
            public void onFinish() {
                isTimerRunning = false;
                if (isViewAttached()) {
                    view.onCountDownFinished();
                }
            }
        }.start();
    }

    @Override
    public void stopCountDown() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            isTimerRunning = false;
        }
    }

    @Override
    public void detachView() {
        stopCountDown();
        super.detachView();
    }
} 